#include <iostream>
#include <vector>

using namespace std;
class Solution
{
public:
    vector<string> commonChars(vector<string> &words)
    {
        vector<string> ans;
        for (auto& c : words[0])
        {
            for (int i = 1; i < words.size(); i++)
            {
               
                if (words[i].find(c) != string::npos && i+1==words.size())
                {
                    ans.push_back(to_string(c));
                }
            }
        }
        return ans;
    }

     vector<string> commonChars_2(vector<string>& words) {
        vector<int> minfreq(26, INT_MAX);
        vector<int> freq(26);
        for (const string& word: words) {
            fill(freq.begin(), freq.end(), 0);
            for (char ch: word) {
                ++freq[ch - 'a'];
            }
            for (int i = 0; i < 26; ++i) {
                minfreq[i] = min(minfreq[i], freq[i]);
            }
        }

        vector<string> ans;
        for (int i = 0; i < 26; ++i) {
            for (int j = 0; j < minfreq[i]; ++j) {
                ans.emplace_back(1, i + 'a');
            }
        }
        return ans;
    }
};
int main()
{
    Solution s;
    vector<string> words = {"bella", "label", "roller"};
    vector<string> ans = s.commonChars(words);
    for (auto &a : ans)
    {
        cout << a << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}
